package love.yuanmengrobot.wuziqi.UI;

import love.yuanmengrobot.wuziqi.UIlzl.Scoreboard;
import love.yuanmengrobot.wuziqi.UIyqh.MallForm;
import love.yuanmengrobot.wuziqi.UIzjj.ShowPacksackPanel;
import love.yuanmengrobot.wuziqi.Util.GetPacksack;

import java.awt.*;
import java.awt.event.*;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
/*
 * Created by JFormDesigner on Mon May 10 19:36:33 CST 2021
 */


/**
 * @author 1
 */
public class StrtForm extends JFrame {
    private String nameLogin;
    private String IdLogin;
    private WuZiQiClient wuZiQiMain;
    static Scoreboard scoreboard;
     static MallForm mallForm;
    private StrtForm strtForm;

    public String getNameLogin() {
        return nameLogin;
    }

    public void setNameLogin(String nameLogin) {
        this.nameLogin = nameLogin;
    }

    public WuZiQiClient getWuZiQiMain() {
        return wuZiQiMain;
    }

    public void setWuZiQiMain(WuZiQiClient wuZiQiMain) {
        this.wuZiQiMain = wuZiQiMain;
    }



    public StartGame getStartGame() {
        return startGame;
    }

    public void setStartGame(StartGame startGame) {
        this.startGame = startGame;
    }

    public StrtForm getStrtForm() {
        return strtForm;
    }

    public void setStrtForm(StrtForm strtForm) {
        this.strtForm = strtForm;
    }

    public String getIdLogin() {
        return IdLogin;
    }

    public void setIdLogin(String idLogin) {
        IdLogin = idLogin;
    }

    //    public Socket socket;
//    static StrtForm strtForm;
    private StartGame startGame;
//
//    public static void main(String[] args) {
//        strtForm = new StrtForm();
//        wuZiQiMain = new WuZiQiClient();
//        strtForm.socket=wuZiQiMain.socket;
//
//        wuZiQiMain.wuZiQiClient=wuZiQiMain;
//        wuZiQiMain.startGame=new StartGame(strtForm.socket);
//        wuZiQiMain.setVisible(false);
//
////        startGame = new StartGame(strtForm.socket);
//
//
//    }

    public StrtForm() {


        initComponents();

//        ImageIcon icon = new ImageIcon("/love/yuanmengrobot/wuziqi/Images/main.png");
//        ImageIcon icon=new ImageIcon("images/psu.jpg");
        //Image im=new Image(icon);
        //将图片放入label中
//        label7 = new JLabel(icon);

        //设置label的大小
//        label7.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
    }

    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void button6BackToHomeActionPerformed(ActionEvent e) {
        // TODO add your code here
        panelMain.setVisible(true);


    }

    private  void initComponents() {
        //label1 = new JLabel("<html><font size=7 color=blue>My</font><font size=7 color=red>Text</font></html>");
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panelMain = new JPanel();
        button1paihang = new JButton();
        button2Pack = new JButton();
        button3Shop = new JButton();
        button4Start = new JButton();
        button5Login = new JButton();
        label9 = new JLabel();
        panel1Register = new JPanel();
        button2RegisterCancel = new JButton();
        label3 = new JLabel();
        textField1QQ = new JTextField();
        textField2UserName = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        button1RegisterAck = new JButton();
        passwordField1pwd1 = new JPasswordField();
        passwordField2pwd2 = new JPasswordField();
        panel1Login = new JPanel();
        label1 = new JLabel();
        textField1UserId = new JTextField();
        label2 = new JLabel();
        button1LoginAck = new JButton();
        button2Register = new JButton();
        button1LoginCancel = new JButton();
        passwordField1UserPwd = new JPasswordField();

        //======== this ========
        setForeground(Color.white);
        setTitle("\u4e3b\u9875");
        setIconImage(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/Avatar.jpg")).getImage());
        setBackground(new Color(222, 222, 222));
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panelMain ========
        {
            panelMain.setBackground(new Color(222, 222, 222));
            panelMain.setLayout(null);

            //---- button1paihang ----
            button1paihang.setIcon(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/score.png")));
            panelMain.add(button1paihang);
            button1paihang.setBounds(20, 390, 165, 50);

            //---- button2Pack ----
            button2Pack.setIcon(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/pack.png")));
            panelMain.add(button2Pack);
            button2Pack.setBounds(705, 360, 135, 60);

            //---- button3Shop ----
            button3Shop.setIcon(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/shop.png")));
            panelMain.add(button3Shop);
            button3Shop.setBounds(750, 15, 80, 50);

            //---- button4Start ----
            button4Start.setText("\u5f00\u59cb\u6e38\u620f");
            button4Start.setIcon(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/startGame.png")));
            panelMain.add(button4Start);
            button4Start.setBounds(255, 260, 355, 80);

            //---- button5Login ----
            button5Login.setIcon(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/longin01.png")));
            panelMain.add(button5Login);
            button5Login.setBounds(725, 285, 90, 45);

            //---- label9 ----
            label9.setIcon(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/main05.png")));
            label9.setBackground(new Color(222, 222, 222));
            panelMain.add(label9);
            label9.setBounds(0, 0, 879, 545);
        }
        contentPane.add(panelMain);
        panelMain.setBounds(0, 0, 879, 545);

        //======== panel1Register ========
        {
            panel1Register.setBackground(new Color(222, 222, 222));
            panel1Register.setLayout(null);

            //---- button2RegisterCancel ----
            button2RegisterCancel.setText("\u53d6\u6d88");
            button2RegisterCancel.setBackground(new Color(222, 222, 222));
            button2RegisterCancel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button2MouseClicked(e);
                }
            });
            panel1Register.add(button2RegisterCancel);
            button2RegisterCancel.setBounds(90, 225, 60, 25);

            //---- label3 ----
            label3.setText("QQ");
            panel1Register.add(label3);
            label3.setBounds(45, 55, 30, 20);
            panel1Register.add(textField1QQ);
            textField1QQ.setBounds(90, 50, 195, 25);
            panel1Register.add(textField2UserName);
            textField2UserName.setBounds(90, 90, 195, 25);

            //---- label4 ----
            label4.setText("\u6635\u79f0");
            panel1Register.add(label4);
            label4.setBounds(35, 95, 55, 17);

            //---- label5 ----
            label5.setText("\u5bc6\u7801");
            panel1Register.add(label5);
            label5.setBounds(45, 130, 40, 17);

            //---- label6 ----
            label6.setText("\u786e\u8ba4\u5bc6\u7801");
            panel1Register.add(label6);
            label6.setBounds(25, 165, 65, 17);

            //---- button1RegisterAck ----
            button1RegisterAck.setText("\u786e\u8ba4");
            button1RegisterAck.setBackground(new Color(222, 222, 222));
            button1RegisterAck.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button1MouseClicked(e);
                }
            });
            panel1Register.add(button1RegisterAck);
            button1RegisterAck.setBounds(215, 225, 60, 25);
            panel1Register.add(passwordField1pwd1);
            passwordField1pwd1.setBounds(90, 125, 195, 25);
            panel1Register.add(passwordField2pwd2);
            passwordField2pwd2.setBounds(90, 160, 195, 25);
        }
        contentPane.add(panel1Register);
        panel1Register.setBounds(241, 50, 370, 300);

        //======== panel1Login ========
        {
            panel1Login.setBackground(new Color(222, 222, 222));
            panel1Login.setLayout(null);

            //---- label1 ----
            label1.setText("\u6635\u79f0");
            panel1Login.add(label1);
            label1.setBounds(65, 70, 45, 17);
            panel1Login.add(textField1UserId);
            textField1UserId.setBounds(115, 70, 145, 20);

            //---- label2 ----
            label2.setText("\u5bc6\u7801");
            panel1Login.add(label2);
            label2.setBounds(65, 120, 45, 17);

            //---- button1LoginAck ----
            button1LoginAck.setText("\u767b\u5f55");
            button1LoginAck.setBackground(new Color(222, 222, 222));
            button1LoginAck.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button1MouseClicked(e);
                }
            });
            panel1Login.add(button1LoginAck);
            button1LoginAck.setBounds(200, 205, 60, 25);

            //---- button2Register ----
            button2Register.setText("\u6ce8\u518c");
            button2Register.setBackground(new Color(222, 222, 222));
            button2Register.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button2MouseClicked(e);
                    button2MouseClicked(e);
                }
            });
            panel1Login.add(button2Register);
            button2Register.setBounds(100, 205, 60, 25);

            //---- button1LoginCancel ----
            button1LoginCancel.setText("\u53d6\u6d88");
            button1LoginCancel.setBackground(new Color(222, 222, 222));
            panel1Login.add(button1LoginCancel);
            button1LoginCancel.setBounds(new Rectangle(new Point(285, 260), button1LoginCancel.getPreferredSize()));
            panel1Login.add(passwordField1UserPwd);
            passwordField1UserPwd.setBounds(115, 125, 145, 20);
        }
        contentPane.add(panel1Login);
        panel1Login.setBounds(241, 50, 370, 300);

        contentPane.setPreferredSize(new Dimension(865, 500));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents


        panel1Login.setVisible(false);
        panel1Register.setVisible(false);
        setVisible(true);


        button5Login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                panel1Login.setVisible(true);
                panel1Register.setVisible(false);
                panelMain.setVisible(false);
            }
        });
        button2RegisterCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel1Login.setVisible(true);
                panelMain.setVisible(false);
                panel1Register.setVisible(false);
            }
        });

        button2Register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel1Login.setVisible(false);
                panel1Register.setVisible(true);
                panelMain.setVisible(false);
            }
        });
        button1LoginCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel1Login.setVisible(false);
                panel1Register.setVisible(false);
                panelMain.setVisible(true);
            }
        });

        //登录确认按钮
        button1LoginAck.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection conn = loginfunction.getConnection();
                Statement stmt = loginfunction.GetStatement(conn);
                String a = textField1UserId.getText();
                String b = new String(passwordField1UserPwd.getPassword());
                String password = null;

                if (a.length() == 0 && b.length() == 0) {
                    JOptionPane.showMessageDialog(null, "账号和密码不能为空！", null, JOptionPane.WARNING_MESSAGE);
                } else if (a.length() == 0) {
                    JOptionPane.showMessageDialog(null, "账号不能为空！", null, JOptionPane.WARNING_MESSAGE);
                } else if (b.length() == 0)
                    JOptionPane.showMessageDialog(null, "密码不能为空！", null, JOptionPane.WARNING_MESSAGE);
                else {
                    try {
                        password = MD5.encoderByMd5(b);
                    } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                        noSuchAlgorithmException.printStackTrace();
                    } catch (UnsupportedEncodingException unsupportedEncodingException) {
                        unsupportedEncodingException.printStackTrace();
                    }
                    String c = loginfunction.selectSql(a, password);
                    ResultSet rs = loginfunction.getresult(stmt, c);
                    boolean flag = loginfunction.selectuser(rs);

                    String getnamesql = loginfunction.getnamesql(a, password);
                    ResultSet rs1 = loginfunction.getresult(stmt, getnamesql);
                    nameLogin = loginfunction.getname(rs1);

                    String getid = loginfunction.getIdsql(a, password);
                    ResultSet rs2 = loginfunction.getresult(stmt, getid);

                    IdLogin = loginfunction.getId(rs2);


                    loginfunction.close(conn, stmt, rs);

                    if (flag == true) {
                        nameLogin=a;
                        JOptionPane.showMessageDialog(null, "登录成功！", null, JOptionPane.WARNING_MESSAGE);

                        button5Login.setIcon(new ImageIcon(getClass().getResource("/love/yuanmengrobot/wuziqi/Images/qie.png")));
                        System.out.println("登录使用的用户名称:" + nameLogin + " id:" + IdLogin);
                        textField1UserId.setText("");
                        passwordField1UserPwd.setText("");
//                        System.out.println("star:"+startGame);
                        startGame.setUserName(a);//
                        startGame.setUserLoginId(IdLogin);

                        panelMain.setVisible(true);
                        panel1Login.setVisible(false);
                        panel1Register.setVisible(false);


                        scoreboard=new Scoreboard();
                        scoreboard.setVisible(false);
                        scoreboard.setStrtForm(strtForm);//让排行榜可以返回主页

                    }
                    if (flag == false) {
                        JOptionPane.showMessageDialog(null, "账号或者密码错误", null, JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });
        //注册确认
        button1RegisterAck.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

//                String
                String a = textField1QQ.getText();
                String b = textField2UserName.getText();
                String c = new String(passwordField1pwd1.getPassword());
                String d = new String(passwordField2pwd2.getPassword());

                if (a == null || b == null || a.length() == 0 || b.length() == 0 || c.length() == 0 || d.length() == 0) {
                    JOptionPane.showMessageDialog(null, "请补全信息！", null, JOptionPane.WARNING_MESSAGE);
                    return;
                }

                if (c.equals(d)) {
                    try {
                        String password = MD5.encoderByMd5(c);
                        String sql = loginfunction.insertsql(a, b, password);
                        Connection conn = loginfunction.getConnection();
                        Statement stmt = loginfunction.GetStatement(conn);
                        boolean flag = loginfunction.adduser(stmt, sql);
                        if (flag == true) {
                            JOptionPane.showMessageDialog(null, "注册成功", null, JOptionPane.WARNING_MESSAGE);
                        }
                        if (flag == false) {
                            JOptionPane.showMessageDialog(null, "注册失败", null, JOptionPane.WARNING_MESSAGE);
                        }
                    } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
                        noSuchAlgorithmException.printStackTrace();
                    } catch (UnsupportedEncodingException unsupportedEncodingException) {
                        unsupportedEncodingException.printStackTrace();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "两次输入的密码不一致，请重新输入！", null, JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        //开始游戏
        button4Start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


                if (nameLogin == null || nameLogin.length() == 0) {
                    JOptionPane.showMessageDialog(null, "请先登录！", null, JOptionPane.WARNING_MESSAGE);

                    panel1Login.setVisible(true);
                    panel1Register.setVisible(false);
                    panelMain.setVisible(false);

                } else {
//                    nameLogin
//                    wuZiQiMain.set
//                    startGame.setUserLoginId(nameLogin);
//                    wuZiQiMain.setVisible(false);
                    startGame.setVisible(true);
                    setVisible(false);
                }
            }
        });
        //前往排行榜
        button1paihang.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                scoreboard.setVisible(true);
                setVisible(false);
            }
        });
        //前往商城
        button3Shop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (nameLogin == null || nameLogin.length() == 0) {
                    JOptionPane.showMessageDialog(null, "请先登录！", null, JOptionPane.WARNING_MESSAGE);
                    panel1Login.setVisible(true);
                    panel1Register.setVisible(false);
                    panelMain.setVisible(false);
                } else {

                    //    static Scoreboard scoreboard;

//
                    mallForm = new MallForm(IdLogin);
                    mallForm.setStrtForm(strtForm);//让商城可以返回主页
                    mallForm.setVisible(true);
                    setVisible(false);
                }
            }
        });

        //打开背包
        button2Pack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (nameLogin == null || nameLogin.length() == 0) {
                    JOptionPane.showMessageDialog(null, "请先登录！", null, JOptionPane.WARNING_MESSAGE);

                    panel1Login.setVisible(true);
                    panel1Register.setVisible(false);
                    panelMain.setVisible(false);

                } else {


                    showPacksackPanel = new ShowPacksackPanel(new GetPacksack().getPacksack(nameLogin));
                    contentPane.add(showPacksackPanel);
                    showPacksackPanel.setBounds(0, 0, 835, 500);
                    showPacksackPanel.setVisible(true);
                    showPacksackPanel.setPanelMain(panelMain);
                    panelMain.setVisible(false);


                }
            }
        });
        setResizable(false);// 设置窗体不可以放大
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭的时候，进程也结束
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panelMain;
    private JButton button1paihang;
    private JButton button2Pack;
    private JButton button3Shop;
    private JButton button4Start;
    private JButton button5Login;
    private JLabel label9;
    private JPanel panel1Register;
    private JButton button2RegisterCancel;
    private JLabel label3;
    private JTextField textField1QQ;
    private JTextField textField2UserName;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JButton button1RegisterAck;
    private JPasswordField passwordField1pwd1;
    private JPasswordField passwordField2pwd2;
    private JPanel panel1Login;
    private JLabel label1;
    private JTextField textField1UserId;
    private JLabel label2;
    private JButton button1LoginAck;
    private JButton button2Register;
    private JButton button1LoginCancel;
    private JPasswordField passwordField1UserPwd;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    ShowPacksackPanel showPacksackPanel;

}
